import { BrowserRouter, Routes, Route } from 'react-router-dom';
import MainLayout from './layouts/MainLayout';
import Dashboard from './pages/Dashboard';
import Settings from './pages/Settings';
import TodoList from './pages/TodoList';
import MarkdownNotes from './pages/MarkdownNotes';
import JsonFormatter from './pages/JsonFormatter';
import GitHubTrending from './pages/GitHubTrending';
import About from './pages/About';
import AgentManager from './pages/AgentManager';
import AgentView from './pages/AgentView';
import AIPlayground from './pages/AIPlayground';
import Login from './pages/Login';
import Register from './pages/Register';
import AiDairy from './pages/AiDairy';
import ExcelViewer from './pages/ExcelViewer';
import Contacts from './pages/Contacts';
import EmailAccounts from './pages/EmailAccounts';
import EmailViewer from './pages/EmailViewer';
import ProtectedRoute from './components/ProtectedRoute';
import { AuthProvider } from './contexts/AuthContext';
import { ThemeProvider } from './lib/theme';

/**
 * 主应用组件 v2.3
 * 支持用户认证、内置微应用、外部 iframe 应用、动态智能体管理和 AI Playground
 */
function App() {
  const features = {
    AUTH: (import.meta as any).env?.VITE_FEATURE_AUTH !== 'false',
    CRUD: (import.meta as any).env?.VITE_FEATURE_CRUD !== 'false',
    EXCEL: (import.meta as any).env?.VITE_FEATURE_EXCEL !== 'false',
    GH_TRENDING: (import.meta as any).env?.VITE_FEATURE_GH_TRENDING !== 'false',
    AI: (import.meta as any).env?.VITE_FEATURE_AI !== 'false',
  };
  return (
    <ThemeProvider>
      <AuthProvider>
        <BrowserRouter>
          <Routes>
            {/* 公开路由 */}
            <Route path="/login" element={<Login />} />
            <Route path="/register" element={<Register />} />
            
            {/* 受保护的路由 */}
            <Route
              path="/"
              element={
                <ProtectedRoute>
                  <MainLayout />
                </ProtectedRoute>
              }
            >
              {/* 首页：仪表板 */}
              <Route index element={<Dashboard />} />
              
              {/* 内置微应用（受功能开关控制） */}
              {features.CRUD && <Route path="app/todo" element={<TodoList />} />}
              {features.CRUD && <Route path="app/notes" element={<MarkdownNotes />} />}
              <Route path="app/json-formatter" element={<JsonFormatter />} />
              {features.GH_TRENDING && <Route path="app/github-trending" element={<GitHubTrending />} />}
              {features.AI && <Route path="app/ai-playground" element={<AIPlayground />} />}
              {features.EXCEL && <Route path="app/excel-viewer" element={<ExcelViewer />} />}
              <Route path="app/about" element={<About />} />
              {features.AI && <Route path="app/ai-dairy" element={<AiDairy />} />}
              {features.CRUD && <Route path="app/contacts" element={<Contacts />} />}
              {features.CRUD && <Route path="app/email-accounts" element={<EmailAccounts />} />}
              {features.CRUD && <Route path="app/email-viewer" element={<EmailViewer />} />}
              {features.AUTH && <Route path="app/settings" element={<Settings />} />}
              
              {/* 智能体管理 */}
              <Route path="app/agents" element={<AgentManager />} />
              <Route path="app/agent/:id" element={<AgentView />} />
            </Route>
          </Routes>
        </BrowserRouter>
      </AuthProvider>
    </ThemeProvider>
  );
}

export default App;
